package com.xinsoft.controller.device;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.CusDevicePageParam;
import com.xinsoft.entity.po.CusDevice;
import com.xinsoft.service.CusDeviceService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/cus/devManagement")
public class CusDeviceController {

    @Autowired
    private CusDeviceService cusDeviceService;

    /**
     * 新增
     */
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> add(@RequestBody CusDevice cusDevice) {
        // 参数校验
        ValidatorUtils.validateEntity(cusDevice);
        return Response.succeed(cusDeviceService.addCusDevice(cusDevice));
    }

    /**
     * 更新
     */
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> edit(@RequestBody  CusDevice cusDevice) {
        // 参数校验
        ValidatorUtils.validateEntity(cusDevice);
        return Response.succeed(cusDeviceService.editCusDevice(cusDevice));
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(cusDeviceService.getCusDevice(id));
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        cusDeviceService.del(ids);
        return Response.succeed();
    }


    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody CusDevicePageParam cusDevicePageParam) {
        return Response.succeed(cusDeviceService.findPageListByParam(cusDevicePageParam));
    }

    /**
     * 不分页
     */
    @PostMapping("/findListByParam")
    public Response<?> findListByParam(@RequestBody CusDevicePageParam cusDevicePageParam) {
        return Response.succeed(cusDeviceService.findListByParam(cusDevicePageParam));
    }


    /**
     * 导入
     */
    @PostMapping("/import")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public Response<?> importExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        return Response.succeed( cusDeviceService.importExcel(multipartFile));
    }

}
